package com.chestnut.contentcore.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chestnut.common.log.annotation.Log;
import com.chestnut.common.log.enums.BusinessType;
import com.chestnut.common.security.web.PageRequest;
import com.chestnut.common.utils.IdUtils;
import com.chestnut.common.utils.StringUtils;
import com.chestnut.contentcore.domain.CmsCatalog;
import com.chestnut.contentcore.domain.CmsContent;
import com.chestnut.contentcore.domain.CmsNewsContent;
import com.chestnut.contentcore.domain.vo.ListContentVO;
import com.chestnut.contentcore.service.ICmsNewsContentService;
import com.chestnut.system.domain.SysDictData;
import com.chestnut.system.mapper.SysDictDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;
import com.chestnut.common.security.web.BaseRestController;
import com.chestnut.common.security.anno.Priv;
import com.chestnut.system.security.AdminUserType;
import lombok.RequiredArgsConstructor;
import com.chestnut.common.domain.R;

/**
 * cms新闻Controller
 *
 * @author chestnut
 * @date 2025-05-30
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/newsContent")
public class CmsNewsContentController extends BaseRestController {

    @Autowired
    private ICmsNewsContentService cmsNewsContentService;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;


    /**
     * 查询cms新闻列表
     */

    @GetMapping("/list")
    public R<?> list(CmsNewsContent cmsNewsContent)
    {
        PageRequest pr = this.getPageRequest();
        LambdaQueryChainWrapper<CmsNewsContent> wrapper = this.cmsNewsContentService.lambdaQuery();
                    if (cmsNewsContent.getLanguage() != null) {
                        wrapper.eq(CmsNewsContent::getLanguage,cmsNewsContent.getLanguage());
                    }
                    if (cmsNewsContent.getType() != null) {
                        wrapper.eq(CmsNewsContent::getType,cmsNewsContent.getType());
                    }
                    if (cmsNewsContent.getTitle() != null) {
                        wrapper.like(CmsNewsContent::getTitle,cmsNewsContent.getTitle());
                    }
        wrapper.orderByAsc(CmsNewsContent::getSort);
        Page<CmsNewsContent> page = wrapper
                .page(new Page<>(pr.getPageNumber(), pr.getPageSize(), true));

        return this.bindDataTable(page);
    }
    @GetMapping("/listNext")
    public R<?> listNext(
                         @RequestParam(required = false) Long dataId,
                         @RequestParam(required = false) String type
    ) {
        LambdaQueryChainWrapper<CmsNewsContent> wrapper = this.cmsNewsContentService.lambdaQuery();
            wrapper.eq(CmsNewsContent::getType,type);
        wrapper.orderByAsc(CmsNewsContent::getSort);
        Page<CmsNewsContent> page = wrapper
                .page(new Page<>(1, 10000, true));
        List<CmsNewsContent> list =page.getRecords();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getId().equals(dataId)){
                if (i == list.size()-1){
                    return	R.ok("null");
                }else{
                    return	R.ok(list.get(i+1).getId(),list.get(i+1).getTitle());
                }
            }
        }
        return	R.ok("null");
    }


    @GetMapping("/getLanguageList/{language}")
    public R<?> getList(@PathVariable("language")  String language) {
      List<SysDictData> sysDictDataList=  sysDictDataMapper.selectList(new QueryWrapper<SysDictData>().eq("remark", language).eq("dict_type","news_types"));

        return  R.ok(sysDictDataList);
    }



    /**
     * 获取cms新闻详细信息
     */

    @GetMapping(value = "/{id}")
    public R<?> getInfo(@PathVariable("id") Long id)
    {
        return R.ok(this.cmsNewsContentService.getById(id));
    }


    @PostMapping
    public R<?> add(@RequestBody CmsNewsContent cmsNewsContent)
    {
        cmsNewsContent.setCreateTime(LocalDateTime.now());
        this.cmsNewsContentService.save(cmsNewsContent);
        return R.ok();
    }

    /**
     * 修改cms新闻
     */
    @Log(title = "cms新闻", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody CmsNewsContent cmsNewsContent)
    {
        this.cmsNewsContentService.updateById(cmsNewsContent);
        return R.ok();
    }

    /**
     * 删除cms新闻
     */
    @Log(title = "cms新闻", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public R<?> remove(@PathVariable Long[] ids)
    {
        this.cmsNewsContentService.removeBatchByIds(List.of(ids));
        return R.ok();
    }
}
